<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<?php
session_start();

if(empty($_SESSION["loginusername"])) {
    Header("Location: firstphp.php");
}

if (!empty($_SESSION["userType"])) {
$userType = $_SESSION["userType"];
}
?>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


        <meta name="description" content="easyui help you build your web page easily!">
        <title>Attendance</title>
        <link href="http://localhost/Eldercare/finalstyle.css" rel="stylesheet" type="text/css" media="all" />
        <script type="text/javascript" src="C:\xampp\htdocs\Eldercare\jquery\jquery-1.6.min.js"></script>
        <script type="text/javascript" src="C:\xampp\htdocs\Eldercare\jquery\jquery.easyui.min.js"></script>
        <script type="text/javascript" src="C:\xampp\htdocs\Eldercare\jquery\jquery.min.js"></script>
        <script src="http://localhost/Eldercare/jquery/demoad.js"></script>
        <script src="http://localhost/Eldercare/jquery/searchbox.js"></script>
        <style type="text/css">
            body {
                background: #FFFFFF url("images/bg.png") repeat;
                background-attachment:fixed;
            }
            .button 
            {
                background-color:beige;
                cursor:pointer; }

            .btn
            {
                background-color:#FFF5EE;
                cursor:pointer;
            }

        </style>
        <script type='text/javascript'>
            var url;
            $(function getAttendance() {
                $.get('Attendance/getattendance.php', {}, function(data) {
                    $('#tblAttendance').replaceWith($(data));
                });
            });
            $(function signoutattendance(x, y) {
                $timeout = NOW();
                $_POST['timeout'] = $timeout;
                $_POST['NRIC'] = y;
                url: 'Attendance/signoutattendance.php?NRIC=' + y + ', timeout=' + $timeout;
            }
            );
            function email()
            {

            }

        </script>
        <script language="Javascript">

            function sent()
            {
                alert('Email Sent!');
            }
        </script>
    </head>
    <body onLoad="document.nricform.barcode.focus();">

        <div class="banner"></div>
        <div id="main">
            <div class="left-wrapper">
                <div class="logo">
                </div>
                <div class="menu-wrapper">
                    <div id="cssmenu">   
                        <ul>
                            <?php
                            if ($userType == "Therapy Assistant") {
                                //if TA
                                echo "<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li class='last'><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			 </ul>
			  </li>
			  <li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
		 <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
   <li class='last'><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
     
			 ";
                            } else if ($userType == "Centre Manager") {
                                echo "
			<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			<li><a href='http://localhost/Eldercare/BackDate.php'><span>BackDate</span></a></li>
			<li class='last'><a href='http://localhost/Eldercare/VerifyAttendance.php'><span>Verify Attendance</span></a></li>
			</ul>
			<li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/User.php'><span>User</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
    <li class='has-sub'><a href='#'><span>Password</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ForgetPasswords.php'><span>Forget Password</span></a></li>
      </ul>
   </li>
			";
                            } else {
                                echo "
			<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			<li><a href='http://localhost/Eldercare/BackDate.php'><span>BackDate</span></a></li>
			<li class='last'><a href='http://localhost/Eldercare/VerifyAttendance.php'><span>Verify Attendance</span></a></li>
			</ul>
			<li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
         <li><a href='http://localhost/Eldercare/User.php'><span>User</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/Centres.php'><span>Centres</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Activities.php'><span>Manage Current Activities</span></a></li>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li><a href='http://localhost/Eldercare/Holidays.php'><span>Holiday</span></a></li>
<li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
    <li class='has-sub'><a href='#'><span>Password</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ForgetPasswords.php'><span>Forget Password</span></a></li>
      </ul>
   </li>
			";
                            }
                            ?>
                        </ul>
                    </div>
                    <div class="clear"></div></div></div>
            <div class="content">
                <div class="content2">
                    
                    <b style="float:right; margin: 0px 0 0 0">  
                        <?php
//session_start();
                        if (!empty($_SESSION["centreName"])) {
                            $Username = $_SESSION["loginusername"];
                            $centreName = $_SESSION["centreName"];
                            echo "Welcome, " . $Username . " ! <br>" . $centreName . " Branch";
                        } else {


                            $Username = $_SESSION["loginusername"];
                            echo "Welcome, " . $Username;
                        }
                        ?>




                        
<input type="submit" class="button" value="Logout" onclick="parent.location = 'logout.php'">
<div class="clear"></div> 
                        <form method="POST" style="text-align:left;vertical-align:left">

                            <input style="border-radius:10px"type="text" name="SearchBox" value=" Search..." onfocus="if (this.value = 'Search...') {
                    this.value = '';
                }"/><input type="submit" button class="btn"name="btnSearch" value="Search"/> 
                        </form>
                        </b>
                    <h3 style="float:left; margin: 0px 0 0 0">Attendance Taking</h3>
                    </p> </p>
                    <table  cellpadding="0" cellspacing="0" style="width: 100%; height: 100px;">
                        <tr>
                            <td style="width: 20%; height: 20%;">
                                <?php
                                if ($userType == "System Admin") {
                                    echo"
<h3>Select a Centre: </h3>

<form  method='POST'>
<select name='ddlCentre'>
	<option value='0'selected >All</option>";

                                    mysql_connect("localhost", "root", "");
                                    mysql_select_db("eldercaredb");
                                    $centreDetail = "SELECT CentreId, CentreName FROM centre";
                                    $query_result = mysql_query($centreDetail);
                                    while ($result = mysql_fetch_assoc($query_result)) {

                                        echo "<option value=";
                                        echo $result['CentreId'];
                                        echo">";
                                        echo $result['CentreName'];
                                        echo"</option>";
                                    }

                                    echo"</select>
		<input type='submit' button class='btn'name='CentreSubmit' value = 'Submit'/>
</form>";
                                }
                                ?>


                                <?php
                                if (!($userType == "System Admin")) {
                                    echo "
                <p style='vertical-align:middle;text-align:center'><b>Scan NRIC: </b><form  method='POST' name='nricform'>&nbsp;&nbsp;<input id='barcode'class='tb10'type='text' name='barcode' size='10' maxLength='9'/>
				</form>";
                                    if (isset($_POST['barcode'])) {
                                        if (strlen($_POST['barcode']) == 9) {
                                            include_once("Attendance/makeattendance.php");
                                        } else {
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>Invalid NRIC!</font>";
                                        }
                                    }
                                    echo "
					
				</p>";
                                }
                                ?>
                            </td>
                            <td rowspan="3" valign="top"align="center"style="width:100%"><p></p>
                                <?php
                                if (isset($_POST['btnSO'])) {
                                    $nric = $_POST['btnSO'];
                                    include 'conn.php';
                                    $checksql = "Select * from attendancerecord WHERE NRIC='$nric' AND date(SignInTimeStamp)=CURDATE() AND Status='Verified'";
                                    $resultcheck = mysql_query($checksql);
                                    if (mysql_num_rows($resultcheck) == 1) {
                                        $sql = "UPDATE attendancerecord SET SignOutTimeStamp=NOW(),AuditSignOut='$Username' WHERE NRIC='$nric' AND DATE(SignInTimeStamp)= CURDATE()";
                                        $result = @mysql_query($sql);
                                    } else {

                                        print '<script type="text/javascript">';
                                        print 'alert("Please Verify the Attendance first!")';
                                        print '</script>';
                                    }
                                    include_once('Attendance/getattendance.php');
                                    echo "<p></p>";
                                }


                                if (isset($_POST['btnSearch'])) {
                                    include_once('Attendance/searchattendance.php');
                                    echo "<p></p>";
                                } else if (isset($_POST['signoutall'])) {
                                    include 'Attendance/conn.php';
                                    $sql1 = "SELECT *
						FROM attendancerecord
						WHERE DATE( SignInTimeStamp ) = CURDATE( ) 
						AND (SignOutTimeStamp is NULL 
						OR SignOutTimeStamp =  '0000-00-00 00:00:00')";
                                    $result1 = @mysql_query($sql1);
                                    if (mysql_num_rows($result1) > 0) {
                                        $checksql = "Select * from attendancerecord WHERE date(SignInTimeStamp)=CURDATE() and Status='Verified' AND (SignOutTimeStamp is NULL 
						OR SignOutTimeStamp =  '0000-00-00 00:00:00') ";
                                        $resultcheck = @mysql_query($checksql);
                                        if (mysql_num_rows($resultcheck) > 0) {
                                            $sql = "UPDATE attendancerecord SET SignOutTimeStamp = NOW(),AuditSignOut='$Username' WHERE (SignOutTimeStamp is NULL or SignOutTimeStamp = '0000-00-00 00:00:00') AND DATE( SignInTimeStamp ) = CURDATE() AND Status='Verified'";
                                            $result = mysql_query($sql);
                                            if ($result) {
                                                print '<script type="text/javascript">';
                                                print 'alert("Verified Attendance has been Signed Out!")';
                                                print '</script>';
                                                include_once("Attendance/getattendance.php");
                                                echo "<p></p>";
                                            }
                                        } else {
                                            print '<script type="text/javascript">';
                                            print 'alert("Please Verify All Attendance first!")';
                                            print '</script>';
                                            include_once("Attendance/getattendance.php");
                                            echo "<p></p>";
                                        }
                                    } else {
                                        print '<script type="text/javascript">';
                                        print 'alert("All Attendance for today has already been Signed Out!")';
                                        print '</script>';
                                        include_once("Attendance/getattendance.php");
                                        echo "<p></p>";
                                    }
                                } else if (isset($_POST['CentreSubmit'])) {
                                    include_once("Attendance/getattendance.php");
                                    echo "<p></p>";
                                } else if (isset($_POST['signoutsl'])) {//1 by 1
                                    if (empty($id) || $id == 0) {
                                        print '<script type="text/javascript">';
                                        print 'alert("Please check one or more boxes first!")';
                                        print '</script>';
                                    } else {
                                        $impid = implode(", ", $id);
                                        $sqlsignout = mysql_query("UPDATE attendancerecord SET SignOutTimeStamp=NOW(),AuditSignOut='$Username' WHERE NRIC IN(" . $impid . ") AND DATE(SignInTimeStamp)= CURDATE()");
                                        if (isset($sqlsignout)) {
                                            include_once("Attendance/getattendance.php");
                                            echo "<p></p>";
                                        }
                                    }
                                } else if (isset($_POST['barcode'])) {
                                    include_once("Attendance/getattendance.php");
                                    echo "<p></p>";
                                } else {
                                    include_once("Attendance/getattendance.php");
                                    echo "<p></p>";
                                }
                                ?>
                            </td>
                        <tr>
                            <td style="vertical-align:middle;text-align:center; width:20%">
                            </td></tr>		<tr></tr>
                        <tr>
                            <td style="width:20%">
                        </tr>
                    </table>


                </div><div class="clear"></div></div>
        </div>
        <?php
        if ($userType == "Centre Manager") {
            if (empty($_SESSION['first'])) {

                mysql_connect("localhost", "root", "");
                mysql_select_db("eldercaredb");
                $today = date('Y-m-d');
                $sql = mysql_query("Select CentreId from centre where CentreName='$centreName'");
                while ($row = mysql_fetch_array($sql)) {
                    $centreId = $row['CentreId'];
                }
                $query = "Select * from Tracking where Date<'$today' and CentreId=$centreId";
                $result = mysql_query($query);
                while ($row = mysql_fetch_array($result)) {

                    if ($row['Status'] == 'Not Exported') {
                        $_SESSION['first'] = 'false';
                        echo "<script language='Javascript'>
                                            if (confirm('A report from last month has not been exported! Would you like to export it?')) {";
                        $_SESSION["centreName"] = $centreName;

                        echo "window.location.href='http://localhost/Eldercare/Attendance/export.php';
                }
                                            </script>";
                        break;
                    }
                }
            }

            if (!empty($_SESSION["sent"]) && ($_SESSION["sent"] == true)) {

                echo "<script language='Javascript'> alert('Monthly Report has been emailed!');</script>";
                $_SESSION["sent"] = false;
            }
        }
        ?>
    </body> 

</html>
